package com.sky.module.suppot.datatrace.util;

import com.sky.module.suppot.datatrace.annotation.History;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @date 2023/3/6 0:32
 **/
public class ContentUtil {


    /***
     * @description 获取到我们的新旧数据  newStr  oldStr
     * @param o1
     * @param o2
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @date 2023/3/5 23:45
     *
     */
    public static Map<String, String> getContent(Object o1, Object o2) {
        AbstractContrast contrast = new AbstractContrast<BaseChangeLog>() {
            @Override
            protected BaseChangeLog initChangeLog() {
                final BaseChangeLog baseChangeLog = new BaseChangeLog();

                baseChangeLog.setDocumentId(1L);
                return baseChangeLog;
            }
        };
        //         比  较字段 可以
        String oldStr = "";
        String newStr = "";
        List<StudentChangeLog> changeLogList = contrast.compareFields(o1, o2);
        for (BaseChangeLog changeLog : changeLogList) {
            oldStr += changeLog.getPropDesc() + ":" + changeLog.getBeforeChange() + "<br/>";
            newStr += changeLog.getPropDesc() + ":" + changeLog.getAfterChange() + "<br/>";
            System.out.print("单据ID为：" + changeLog.getDocumentId());
            System.out.print("，属性名为：" + changeLog.getPropName());
            System.out.print("，属性描述为：" + changeLog.getPropDesc());
            System.out.print("，变更前为：" + changeLog.getBeforeChange());
            System.out.print("，变更后为：" + changeLog.getAfterChange());
            //
            System.out.println();
        }
        System.out.println(oldStr);
        System.out.println(newStr);
        Map<String, String> map = new HashMap<>();
        map.put("oldStr", oldStr);
        map.put("newStr", newStr);
        return map;
    }

    /***
     * @description 根据对象获取表名
     * @param obj
     * @return java.lang.String
     * @date 2023/3/6 0:34

     */
    public static String getTableName(Object obj) {
        History history = obj.getClass().getAnnotation(History.class);
        if (history != null) {
            String table = history.table();
            return table;
        }
        return "";
    }
}
